#ifndef IOCTRL
#define IOCTRL

#include <QSerialPort>
#include <QSerialPortInfo>
#include <QString>
#include <QTimer>
namespace WAY
{
    enum
    {
        Bit   = 0,
        Group,
        All
    };
}
namespace COMMOND
{
    enum
    {
        BITOPEN  = 0x01,
        BITCLOSE = 0x02,
        ALLOPEN  = 0x03,
        ALLCLOSE = 0x04,
        GROUPOPEN  = 0x05,
        GROUPCLOSE = 0x06,
        READSTATUS = 0x07,
        READADDRESS = 0x08,
        SETADDRESS  = 0x09,
        CLOSE       = 0x12
    };
}

class IoCtrl : public QObject
{
    Q_OBJECT
public:
    IoCtrl();
    bool open(QString strCom);
    bool close();
    bool sendcommand(int nWay,bool bOpen,quint8 nData);
signals:
    void sigMsg(const QString& strErr);
    void sigInput(int nIndex,bool bResult);
private:
    bool bitBarrier(bool bOpen,quint8 nData);
    bool groupBarrier(bool bOpen,quint8 nData);
    bool allBarrier(bool bOpen);
    void wrapFrame(QByteArray &data);
    quint8 getCheckSum(QByteArray &data);
    void processFrame(QByteArray &data);

private slots:
    void serialDataArrived();
    void slotReadStatus();

private:
    QSerialPort *m_pSerialPort;
    QTimer      *m_pTimer;
    QByteArray  data;
};

#endif // IOCTRL

